//
//  AbsPresentViewController.m
//  ERP
//
//  Created by goldeney on 15/4/24.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "AbsPresentViewController.h"

#define NAVIGATION_HEIGHT (CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + CGRectGetHeight(self.navigationController.navigationBar.frame))
#define STATUSBAR_HEIGHT (CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]))

@interface AbsPresentViewController ()

@property (nonatomic, strong) UINavigationBar *navigationBar;

@end

@implementation AbsPresentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        self.view.backgroundColor = ThemeColorBlue;
        self.navigationBar = [[UINavigationBar alloc] init];
        self.navigationBar.frame = CGRectMake(0, STATUSBAR_HEIGHT,SuperWidth, NAVIGATION_HEIGHT);
        
//        self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, NAVIGATION_HEIGHT)];
//        [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//        self.navigationBar.translucent = NO;
//        [self.navigationBar setBackgroundColor:ThemeColorBlue];
        
        UIImage *img = [self createImageWithColor:ThemeColorBlue size:CGSizeMake(1, 1)];
        [self.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
        [self setStatusBarBackgroundColor:ThemeColorBlue];

//        UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.navigationBar.bounds), CGRectGetHeight(self.navigationBar.bounds))];
//        overlay.userInteractionEnabled = NO;
//        overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
//        overlay.backgroundColor = ThemeColorBlue;
//        [[self.navigationBar.subviews firstObject] insertSubview:overlay atIndex:0];

        [self.navigationBar setShadowImage:[[UIImage alloc] init]];
        [self.navigationBar setBarStyle:UIBarStyleBlackOpaque];
        [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName, nil]];
        [self.view addSubview:self.navigationBar];

        UIImage *backImage = [UIImage imageNamed:@"skin_header_btn_back_normal.png"];
        UIImage *highlightImage = [UIImage imageNamed:@"skin_header_btn_back_press.png"];

        CGRect backframe = CGRectMake(-20,0,50,44);
        UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
        [backButton setImage:backImage forState:UIControlStateNormal];
        [backButton setImage:highlightImage forState:UIControlStateHighlighted];
        [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
//        backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];

        UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"无标题"];
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        navigationItem.leftBarButtonItem = leftBarButtonItem;
        self.navigationBar.items = @[navigationItem];
    }
    return self;
}

//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

- (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size {
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //为了不影响其他页面在viewDidDisappear做以下设置
    self.navigationController.navigationBar.translucent = YES;//透明
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}

- (id)initWithValue:(id)object
{
    self = [super init];
    if (self) {
        self.value = object;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)setTitle:(NSString *)title
{
    UINavigationItem *navigationItem = self.navigationBar.items.firstObject;
    navigationItem.title = title;
}

- (void)addViewController:(UIViewController *)childController
{
//    CGRect frame = childController.view.frame;
//    frame.origin.y = 64;
//    frame.size.height -= 64;
//    childController.view.frame = frame;
    [self.view addSubview:childController.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark 返回数据事件方法
- (void)getPassingParameter:(id)object
{
    
}

#pragma mark -
#pragma mark IBAction Event
- (void)backButtonEvent:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)done:(id)sender
{
    
}

@end
